#include "STabCtrl.h"
#include <QColor>
#include <qtooltip.h>
#include <QHBoxLayout>
#include <core/StyleMgr.h>
#include <core/ImageMgr.h>
#include <QStylePainter>
#include <QRect>


STabLabel::STabLabel(const QString& t, QWidget* parent /*= nullptr*/)
	: QLabel(t, parent)
{
	setAlignment(Qt::AlignCenter);
	setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
	setMouseTracking(true);
}

STabLabel::~STabLabel()
{

}

void STabLabel::mousePressEvent(QMouseEvent* event)
{
	if (event->button() == Qt::LeftButton)
	{
		emit clicked(m_index);
	}

	return QLabel::mousePressEvent(event);
}

void STabLabel::paintEvent(QPaintEvent* event)
{
	QLabel::paintEvent(event);
	
	if (m_imgName != "")
	{
		QStylePainter painter(this);
		SImage* img = ImgMgr::instance().image(m_imgName, WidgetStatus::kNormal, devicePixelRatioF());
		
		QFontMetrics fm(font());
		int width = fm.horizontalAdvance(text());

		int x = (rect().center().x() + width / 2) + 2;
		int y = rect().center().y() - img->height() / 2;

		m_ImageRect.setRect(x, y, img->width(), img->height());

		if (img)
			painter.drawImage(m_ImageRect, *img);
	}
}

void STabLabel::mouseMoveEvent(QMouseEvent* event)
{
	if (m_ImageRect.contains(event->pos()))
	{
		QToolTip::showText(event->globalPos(), m_imgTip);
	}
	else
	{
		QToolTip::hideText();
	}
}

STabCtrl::STabCtrl(QWidget* p)
	: QFrame(p)
{
	QHBoxLayout* l = new QHBoxLayout(this);
	l->setSpacing(0);
	l->setContentsMargins(0, 0, 0, 0);

	m_styleSheet = QString(
		"STabCtrl {border:1px solid #%6;border-radius:2px;}"
		"STabLabel						{color:#%1; background-color:#%2;border:none;}"
		"STabLabel:hover				{background-color:#%4;}"
		"STabLabel:disabled				{color:#%5; background:transparent;}"
		"STabLabel[TabPos=\"left\"]	{border-top-left-radius:2px; border-bottom-left-radius:2px; border-right:none;}"
		"STabLabel[TabPos=\"right\"]   {border-top-right-radius:2px; border-bottom-right-radius:2px; border-left:none;}"
		"STabLabel[selected=\"true\"]	{background-color:#%3;}"
	)
		.arg(StyleMgr::instance().color("TextNormal").rgb(), 0, 16)
		.arg(StyleMgr::instance().color("BackNormal").rgb(), 0, 16)
		.arg(StyleMgr::instance().color("ButtonNormal").rgb(), 0, 16)
		.arg(StyleMgr::instance().color("Hover").rgb(), 0, 16)
		.arg(StyleMgr::instance().color("TxtDisabled").rgb(), 0, 16)
		.arg(StyleMgr::instance().color("BorderNormal").rgb(), 0, 16);

	setStyleSheet(m_styleSheet);
}

void STabCtrl::addTab(const QString& name,const QString& tip)
{
	STabLabel* item = new STabLabel(name, this);

	m_labels.push_back(item);
	if(!tip.isEmpty())
		item->setToolTip(tip);
	item->setIndex(m_labels.size() - 1);
	layout()->addWidget(item);

	connect(item, &STabLabel::clicked, this, &STabCtrl::onTabClicked);
}

void STabCtrl::setTabBkColor(int index,QColor color)
{
	m_bkColors[index] = color;
}

void STabCtrl::setTabInfo(int index, const QString& imgName, const QString& tip /*= ""*/)
{
	m_labels[index]->setImgName(imgName);
	m_labels[index]->setImgTip(tip);
}

void STabCtrl::selectTab(int index)
{
	//index为-1的时候，默认不选Tab
	if (index >= (int)m_labels.size() || m_labels.size() <= 0 || index == m_selIndex)return;
	m_selIndex = index;
	for (int i = 0; i < (int)m_labels.size(); ++i)
	{
		m_labels[i]->setProperty("selected", (i == index) ? true : false);
	}

	QColor bkColor = StyleMgr::instance().color("ButtonNormal");
	bool useSpecialBkColor = !m_bkColors.empty();
	if (useSpecialBkColor)
	{
		auto clr = m_bkColors.find(index);
		if (clr != m_bkColors.end())
			bkColor = clr->second;
	}
	m_styleSheet = QString(
		"STabCtrl {border:1px solid #%6;border-radius:2px;}"
		"STabLabel						{color:#%1; background-color:#%2;border:none;}"
		"STabLabel:hover				{background-color:#%4;}"
		"STabLabel:disabled				{color:#%5; background:transparent;}"
		"STabLabel[TabPos = \"left\"]  {border-top-left-radius:2px;border-bottom-left-radius:2px;border-right:none;}"
		"STabLabel[TabPos = \"right\"] {border-top-right-radius:2px;border-bottom-right-radius:2px;border-left:none;}"
		"STabLabel[selected=\"true\"]	{background-color:#%3}"
	)
		.arg(StyleMgr::instance().color("TextNormal").rgb(), 0, 16)
		.arg(StyleMgr::instance().color("BackNormal").rgb(), 0, 16)
		.arg(bkColor.rgb(), 0, 16)
		.arg(StyleMgr::instance().color("Hover").rgb(), 0, 16)
		.arg(StyleMgr::instance().color("TxtDisabled").rgb(), 0, 16)
		.arg(StyleMgr::instance().color("BorderNormal").rgb(), 0, 16);

	setStyleSheet(m_styleSheet);
}

void STabCtrl::setTabDisabled(int index)
{
	for (int i = 0; i < (int)m_labels.size(); ++i)
	{
		if (index == i)
			m_labels[i]->setDisabled(true);
	}
	setStyleSheet(m_styleSheet);
}

void STabCtrl::setTabText(int index, const QString& name)
{
	if (index >= (int)m_labels.size())
		return;

	m_labels[index]->setText(name);
}

int STabCtrl::getTabCount()
{
	return m_labels.size();
}

int STabCtrl::getSelTab()
{
	return m_selIndex;
}

void STabCtrl::setTabData(int index, QVariant v)
{
	if (index >= (int)m_labels.size())
		return;

	m_labels[index]->setProperty("data", v);
}

QVariant STabCtrl::tabData(int index)
{
	if (index >= (int)m_labels.size())
		return QVariant();

	return m_labels[index]->property("data");
}

void STabCtrl::onTabClicked(int index)
{
	selectTab(index);
	emit tabSelected(index);
}

void STabCtrl::showEvent(QShowEvent* event)
{
	m_labels[0]->setProperty("TabPos", "left");
	m_labels[m_labels.size() - 1]->setProperty("TabPos", "right");

	setStyleSheet(m_styleSheet);

	QFrame::showEvent(event);
}
